package edu.ybu.manage.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ybu.manage.pojo.CangKu;
import edu.ybu.manage.pojo.Dealer;
import edu.ybu.manage.service.CangKuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cangku")
public class CangkuController {
    @Autowired
    private CangKuService cangKuService;
    @RequestMapping("/selectAll")
    public Map<String,Object> selectAll(){
        List<CangKu> cangKus = cangKuService.selectAll();
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("code", 0);
        res.put("msg", "");
        res.put("count", cangKus.size());
        res.put("data",cangKus );
        return  res;
    }
    @RequestMapping("/updateCangKu")
    public Integer updateCangKu(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        CangKu cangKu = null;
        try {
            cangKu = objectMapper.readValue(json, CangKu.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        return cangKuService.updateCangKu(cangKu);
    }
    @RequestMapping("/deleteById")
    public Integer deleteById(Integer id){
        return cangKuService.deleteById(id);
    }
    @RequestMapping("/insertCangKu")
    public Integer insertCangKu(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        CangKu cangKu = null;
        try {
            cangKu = objectMapper.readValue(json, CangKu.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        if (cangKu==null){
            return 0;
        }
        return cangKuService.insertCangKu(cangKu);
    }
}
